﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace AppEszköz.Cryptography
{
    public sealed class TripleDesEncryption
    {
        private static byte[] IV = new byte[8]
        {
      (byte) 240,
      (byte) 3,
      (byte) 45,
      (byte) 29,
      (byte) 0,
      (byte) 76,
      (byte) 173,
      (byte) 59
        };

        private TripleDesEncryption()
        {
        }

        public static string Encrypt(string plainText, string key)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(plainText);
            TripleDESCryptoServiceProvider cryptoServiceProvider1 = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider cryptoServiceProvider2 = new MD5CryptoServiceProvider();
            cryptoServiceProvider1.Key = cryptoServiceProvider2.ComputeHash(Encoding.ASCII.GetBytes(key));
            cryptoServiceProvider1.IV = TripleDesEncryption.IV;
            return Convert.ToBase64String(cryptoServiceProvider1.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
        }

        public static string Decrypt(string encryptedText, string key)
        {
            byte[] inputBuffer = Convert.FromBase64String(encryptedText);
            TripleDESCryptoServiceProvider cryptoServiceProvider1 = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider cryptoServiceProvider2 = new MD5CryptoServiceProvider();
            cryptoServiceProvider1.Key = cryptoServiceProvider2.ComputeHash(Encoding.ASCII.GetBytes(key));
            cryptoServiceProvider1.IV = TripleDesEncryption.IV;
            return Encoding.ASCII.GetString(cryptoServiceProvider1.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
        }
    }
}
